const API_BASE = 'http://localhost:8080/api/protected';

export async function getCurrentUser() {
  const token = localStorage.getItem('token');
  const res = await fetch(`${API_BASE}/me`, {
    headers: { 'Authorization': 'Bearer ' + token }
  });
  return await res.json();
}

export async function updateUser({ signature, avatar }) {
  const token = localStorage.getItem('token');
  const res = await fetch(`${API_BASE}/update`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + token
    },
    body: JSON.stringify({ signature, avatar })
  });
  return await res.json();
}

export async function uploadAvatar(file) {
  const token = localStorage.getItem('token');
  const formData = new FormData();
  formData.append('avatar', file);
  const res = await fetch(`${API_BASE}/upload-avatar`, {
    method: 'POST',
    headers: { 'Authorization': 'Bearer ' + token },
    body: formData
  });
  return await res.json();
}

// 简历
export async function getResumes(page, size) {
  const token = localStorage.getItem('token');
  const res = await fetch(`${API_BASE}/resume/list?page=${page}&size=${size}`, {
    method: 'GET',
    headers: { 'Authorization': 'Bearer ' + token }
  });
  return await res.json();
}

export async function uploadResume(file) {
  const token = localStorage.getItem('token');
  const formData = new FormData();
  formData.append('file', file);
  const res = await fetch(`${API_BASE}/resume/upload`, {
    method: 'POST',
    headers: { 'Authorization': 'Bearer ' + token },
    body: formData
  }); 
  return await res.json();
}

export async function deleteResume(id) {
  const token = localStorage.getItem('token');
  const res = await fetch(`${API_BASE}/resume/${id}`, {
    method: 'DELETE',
    headers: { 'Authorization': 'Bearer ' + token }
  });
  return await res.json();
}

export async function setDefaultResume(id) {
  const token = localStorage.getItem('token');
  const res = await fetch(`${API_BASE}/resume/set-default/${id}`, {
    method: 'POST',
    headers: { 'Authorization': 'Bearer ' + token }
  });
  return await res.json();
}